package com.junior.fixclub;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import java.net.InetAddress;
import java.util.Random;

public class ParamMaker {
    //Build
    public static String BOARD = "sailfish";
    public static String BRAND = "google";
    public static String DEVICE = "sailfish";
    public static String HARDWARE = "sailfish";
    public static String MANUFACTURER = "Google";
    public static String MODEL = "Pixel";
    public static String PRODUCT = "sailfish";
    public static String TAGS = "release-keys";
    public static String TYPE = "user";
    public static String NAME = "sailfish";

    public static String SERIAL = "unknown";
    public static String BASEBAND = "8996-130361-1905270421";
    public static String DISPLAY = "QP1A.191005.007.A3";
    public static String ID = "QP1A.191005.007.A3";
    public static String DESCRIPTION = "";//description
    public static String BOOTLOADER = "8996-012001-1908071822";
    public static String HOST = "abfarm792";
    public static String INCREMENTAL = "5972272";
    public static long   TIME = 1572305593000L;
    public static String ANDROID_ID = "6a930572d1fb9b36";
    public static String BLUETOOTHMAC = "BC:1A:EA:D9:8D:98";
    public static String WIFIMAC = "AC:37:43:4C:B2:32";
    public static int    WIFIIP = 0;
    public static String WIFINAME = "MiWifi";
    public static String WIFIBSSID = "ce:ea:8c:1a:5c:b2";
    public static String FINGERPRINT = "google/sailfish/sailfish:10/"+DISPLAY+"/"+INCREMENTAL+":user/release-keys";
    public static String USERAGENT = "Dalvik/2.1.0 (Linux; U; Android 10; Pixel Build/"+DISPLAY+")";

    public static String build_version_emui = "";
    public static String build_miui_version_name = "";
    public static String build_vivo_build_display_id = "";
    public static String build_vivo_product_version = "";
    public static String build_opporom = "";
    public static String build_uiversion_360 = "";
    public static String build_jinli_sv_version = "";
    public static String build_letv_release_version = "";

    public static String IMEI1 = "860735036833796";
    public static String IMEI2 = "860735036833783";
    public static String MEID = "9900117939338";

    public static String CPU_ABI = "armeabi-v7a";
    public static String CPU_ABI2 = "armeabi";
    public static String ABI_LIST = "arm64-v8a,armeabi-v7a,armeabi";
    public static String RELEASE_VERSION = "10";
    public static int    SDK_INT = 29;
    public static String SDK = "29";
    public static String CODENAME = "REL";

    public static String PLATFORM = "";

    public static String DPI = "0";
    public static float DENSITY = 0.0f;
    public static float XDPI = 0.0f;
    public static float YDPI = 0.0f;
    public static float SCALED_DENSITY = 0.0f;
    public static int WIDTH = 0;
    public static int HEIGHT = 0;

    public static String cpuinfo_max_freq = "";
    public static String cpuinfo_min_freq = "";

    public static void make(){
        JSONObject object = JSON.parseObject(getDeviceJson(2));
        BOARD = object.getString("build_board");
        BRAND = object.getString("build_brand");
        DEVICE = object.getString("build_board");
        HARDWARE = object.getString("build_board");
        MANUFACTURER = object.getString("build_board");
        MODEL = object.getString("build_model");
        PRODUCT = object.getString("build_product");
        NAME = object.getString("build_product");
        TAGS = "release-keys";
        TYPE = object.getString("build_type");
        CPU_ABI = object.getString("build_cpu_abi");
        CPU_ABI2 = object.getString("build_cpu_abi2");
        RELEASE_VERSION = object.getString("build_version_release");
        SDK_INT = object.getIntValue("build_version_sdk_int");
        SDK = String.valueOf(object.getIntValue("build_version_sdk_int"));
        CODENAME = object.getString("build_version_codename");

        FINGERPRINT = object.getString("build_fingerprint");
        USERAGENT = object.getString("webview_user_agent");
        SERIAL = object.getString("build_serial");//"unknown";
        BASEBAND = object.getString("build_baseband");//"8696-130361-"+getRandomStr(CHAR_TABLE_10,10);
        //String rand = getRandomStr(CHAR_TABLE_10,4);
        DISPLAY = object.getString("build_display");//"QP1A.19"+rand+".007.A3";
        ID = object.getString("build_id");//"QP1A.19"+rand+".007.A3";
        DESCRIPTION = object.getString("build_description");
        BOOTLOADER = object.getString("build_bootloader");//"8696-012001-"+getRandomStr(CHAR_TABLE_10,10);
        HOST = object.getString("build_host");//"abfarm792";
        INCREMENTAL = object.getString("build_version_incremental");//getRandomStr(CHAR_TABLE_10,7);
        TIME = object.getLong("build_time")+nextInt(10000,100000);

        DPI = object.getString("display_densitydpi");
        DENSITY = object.getFloat("display_density");
        XDPI = object.getFloat("display_xdpi");
        YDPI = object.getFloat("display_ydpi");
        SCALED_DENSITY = object.getFloat("display_scaled_density");
        WIDTH = object.getInteger("display_width");
        HEIGHT = object.getInteger("display_height");
        ANDROID_ID = getRandomStr(CHAR_TABLE_16,16);
        BLUETOOTHMAC = getRandomMacAddr().toUpperCase();
        WIFIMAC =  getRandomMacAddr().toUpperCase();
        WIFIIP = ipToInt(getRandomIp());
        WIFINAME = "Wifi-"+getRandomStr(CHAR_TABLE_62,6);
        WIFIBSSID = generate("ce:ea:8c");
        IMEI1 = generateImei();
        IMEI2 = generateImei();
        MEID = "9900"+getRandomStr(CHAR_TABLE_10,9);

        build_version_emui = object.getString("build_version_emui");
        build_miui_version_name = object.getString("build_miui_version_name");
        build_vivo_build_display_id = object.getString("build_vivo_build_display_id");
        build_vivo_product_version = object.getString("build_vivo_product_version");
        build_opporom = object.getString("build_opporom");
        build_uiversion_360 = object.getString("build_uiversion_360");
        build_jinli_sv_version = object.getString("build_jinli_sv_version");
        build_letv_release_version = object.getString("build_letv_release_version");

        cpuinfo_max_freq = object.getString("");
        cpuinfo_min_freq = object.getString("");
    }



    private static String getDeviceJson(int key) {
        switch (key){
            case 2:
                return Devices.dev_2;
            case 3:
                return Devices.dev_3;
            case 4:
                return Devices.dev_4;
            case 5:
                return Devices.dev_5;
            case 6:
                return Devices.dev_6;
            default:
                return Devices.dev_7;
        }
    }


    public static char[] CHAR_TABLE_62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
    public static char[] CHAR_TABLE_36 = "0123456789abcdefghijklmnopqrstuvwxyz".toCharArray();
    public static char[] CHAR_TABLE_16 = "0123456789abcdef".toCharArray();
    public static char[] CHAR_TABLE_10 = "0123456789".toCharArray();

    public static Random random = new Random();

    public static String generateImei() {
        String imei = "860735";
        imei += getRandomStr10(8);
        imei += "0";
        return sign(imei);
    }

    public static boolean check(String ccNumber) {
        int sum = 0;
        boolean alternate = false;
        for (int i = ccNumber.length() - 1; i >= 0; i--) {
            int n = Integer.parseInt(ccNumber.substring(i, i + 1));
            if (alternate) {
                n *= 2;
                if (n > 9) {
                    n = (n % 10) + 1;
                }
            }
            sum += n;
            alternate = !alternate;
        }
        return (sum % 10 == 0);
    }

    public static String sign(String ccNumber) {
        if(StringUtils.isBlank(ccNumber))
            return null;
        int sum = 0;
        boolean alternate = false;
        char imeiArr[] = ccNumber.toCharArray();

        for (int i = imeiArr.length - 1; i >= 0; i--) {
            int n = imeiArr[i] - 0x30;
            if(n < 0 || n > 9)
                return null;

            if (alternate) {
                n *= 2;
                if (n > 9) {
                    n = (n % 10) + 1;
                }
            }
            sum += n;
            alternate = !alternate;
        }
        int finalInt = 10 - (sum % 10);

        imeiArr[imeiArr.length-1] = (char) (finalInt + 0x30);
        return new String(imeiArr);
    }
    public static String getRandomStr10(int length) {
        return getRandomStr(CHAR_TABLE_10, length);
    }
    public static String getRandomStr(char table[], int length) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int seed = random.nextInt(table.length);
            sb.append(String.valueOf(table[seed]));
        }
        return sb.toString();
    }

    public static byte[] getRandomBytes(int length) {
        byte[] result = new byte[length];
        random.nextBytes(result);
        return result;
    }

    public static String getRandomMacAddr() {
        byte[] addr = getRandomBytes(6);
        return String.format("%02x:%02x:%02x:%02x:%02x:%02x", addr[0],addr[1],addr[2], addr[3],addr[4],addr[5]);
    }

    public static String generate(String prefix)
    {
        String result = prefix + ":" + generate(3);
        return result;
    }

    private static String generate(int radix)
    {
        byte[] bytes = getRandomBytes(radix);
        StringBuilder rand = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            String s = String.format("%02x", bytes[i]);
            rand.append(s);
            rand.append(":");
        }
        String str = rand.toString();
        return str.substring(0,str.length()-1);
    }

    public static String generate()
    {
        return generate(6);
    }

    public static String getRandomIp() {
        int three = nextInt(1, 255);
        int four = nextInt(1, 255);
        String localIp = "192.168."+three+"."+four;
        return localIp;
    }

    public static int nextInt(int min, int max) {
        if (max <= min) {
            return min;
        }
        return min + (random.nextInt(max) % (max - min));
    }

    public static int ipToInt(String ipAddr) {
        try {
            return bytesToInt(ipToBytesByInet(ipAddr));
        } catch (Exception e) {
            throw new IllegalArgumentException(ipAddr + " is invalid IP");
        }
    }

    public static byte[] ipToBytesByInet(String ipAddr) {
        try {
            return InetAddress.getByName(ipAddr).getAddress();
        } catch (Exception e) {
            throw new IllegalArgumentException(ipAddr + " is invalid IP");
        }
    }

    public static int bytesToInt(byte[] bytes) {
        int addr = bytes[3] & 0xFF;
        addr |= ((bytes[2] << 8) & 0xFF00);
        addr |= ((bytes[1] << 16) & 0xFF0000);
        addr |= ((bytes[0] << 24) & 0xFF000000);
        return addr;
    }


}
